package org.example.sb_20241005springboot.controller;

import org.example.sb_20241005springboot.model.MessageInfo;
import org.example.sb_20241005springboot.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:做渐进主义的改良派,不做完没注意的速成派
 * User: XALEXC
 * Date: 2024-10-22
 * Time: 10:40
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
   private MessageService messageService;
    private List<MessageInfo> messageInfos=new ArrayList<>();

    @RequestMapping("/publish")
    public Boolean publishMessage(MessageInfo messageInfo){
        // 进行参数的校验
        if (!StringUtils.hasLength(messageInfo.getFrom())||
                !StringUtils.hasLength(messageInfo.getTo())||
                !StringUtils.hasLength(messageInfo.getMessage())){
            return false;
        }
        // 添加留言
        messageService.addMessage(messageInfo);
        return true;
    }

    @RequestMapping("/getMessageInfo")
    public List<MessageInfo> getMessageInfo(){
        return messageService.getMessage();

    }
}
